﻿CREATE PROCEDURE [ServiceEngine].[AssignSubjectToAssertionType]
 @AssertionTypeID uniqueidentifier,
 @EntityTypeID smallint
AS
BEGIN
 DECLARE @ErrorMessage NVARCHAR(4000);
 DECLARE @ErrorSeverity INT;
 DECLARE @ErrorState INT;
 SET NOCOUNT ON;

 BEGIN TRY
  BEGIN TRANSACTION  
  
  SELECT AssertionTypeID
  FROM ServiceEngine.AssertionTypes_Subjects
  WHERE AssertionTypeID = @AssertionTypeID and
  EntityTypeID = @EntityTypeID
  
  IF (@@ROWCOUNT = 0) 
  BEGIN   
   INSERT ServiceEngine.AssertionTypes_Subjects (AssertionTypeID, EntityTypeID)
   VALUES (@AssertionTypeID, @EntityTypeID)
  END

  COMMIT TRANSACTION
 END TRY
 BEGIN CATCH
  SELECT 
   @ErrorMessage = ERROR_MESSAGE(),
   @ErrorSeverity = ERROR_SEVERITY(),
   @ErrorState = ERROR_STATE();

  IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

  RAISERROR (@ErrorMessage, 
       @ErrorSeverity,
       1);
 END CATCH

END
